<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>加油信息登记</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layui/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form" method="post" id="applyForm">
    <div class="layui-form-item">
        <label class="layui-form-label">车牌号码</label>
        <div class="layui-input-block">
            <select id="carid" name="carid" lay-filter="carid">

            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">加油站</label>
        <div class="layui-input-block">
            <select id="btypeid" name="btypeid">
                <option></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">加油时间:</label>
        <div class="layui-input-block">
            <input type="text" id="petroltime" name="petroltime" autocomplete="off" required  lay-verify="required" placeholder="请输入加油时间" class="layui-input usetime">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">油料标号:</label>
        <div class="layui-input-block">
            <select id="pid" name="pid">
                <option value="0">请选择</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">油料单价(每升):</label>
        <div class="layui-input-block">
            <input type="text" id="petrolprice" name="petrolprice" required  lay-verify="required" placeholder="请输入油料单价" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">本次加油(升):</label>
        <div class="layui-input-block">
            <input type="text" id="petrolnums" name="petrolnums" required  lay-verify="required" placeholder="请输入本次加油量" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">加油总价(升):</label>
        <div class="layui-input-block">
            <input type="text" id="petroltotalprice" name="petroltotalprice" required  lay-verify="required" placeholder="请输入目的地" autocomplete="off" class="layui-input" readonly="readonly">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">本次里程(公里):</label>
        <div class="layui-input-block">
            <input type="text" id="petrolstartkm" name="petrolstartkm" required  lay-verify="required" placeholder="请输入本次里程" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上次里程(公里):</label>
        <div class="layui-input-block">
            <input type="text" id="petrolendkm" required  lay-verify="required" autocomplete="off" class="layui-input" disabled>
            <input type="hidden" id="petrolendkmValue" name="petrolendkm">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上次加油(升):</label>
        <div class="layui-input-block">
            <input type="text" id="lastpertrolnums"  required  lay-verify="required"  autocomplete="off" class="layui-input" disabled>
            <input type="hidden" id="lastpetrrolnumsValue" name="lastpetrolnums">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">加油人员:</label>
        <div class="layui-input-block">
            <input type="text" id="drivername"  required  lay-verify="required" autocomplete="off" class="layui-input" disabled>
            <input type="hidden" id="driverid" name="driverid">
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="applyAdd" id="addBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/layui/layui/layui.js"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl','upload'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table,
            upload=layui.upload;

        //绑定时间插件到文本框中

            laydate.render({
                elem:'#petroltime',
                theme: 'grid',
                type: 'datetime'
            });

        //提交表单
        form.on("submit(applyAdd)",function(data) {
            // var formData = new FormData($("#applyForm")[0]);
            $.ajax({
                url:"/petrolRecord/petrolRecordAdd",
                type: "post",
                data:$("#applyForm").serialize(),
                success:function (res) {
                    if(res.code == 1001){
                        layui.layer.msg(res.msg);
                    }else{
                        layui.layer.msg(res.msg);
                    }
                }
            });
            //点击提交后关闭悬浮窗
            setTimeout(function () {
                window.parent.location.reload();
            },1500);
            return false;
        });

        //监听车牌号的select
        form.on('select(carid)', function(data){
            $.ajax({
                url:"/car/car",
                type:"post",
                data:{"carid":data.value},
                success:function (res) {
                    $("#drivername").attr("value",res.data.driver.dirvername);
                    $("#driverid").attr("value",res.data.driver.driverid);
                }
            });

            $.ajax({
                url:"/petrolRecord/findFinalRecord",
                type:"post",
                data:{"carid":data.value},
                success:function (res) {
                    if(null != res && "" != res){
                        $("#petrolprice").attr("value",res.petrolprice);
                        $("#petrolendkm").attr("value",res.petrolstartkm);
                        $("#lastpertrolnums").attr("value",res.petrolnums);
                        $("#lastpetrrolnumsValue").attr("value",res.petrolnums);
                        $("#petrolendkmValue").attr("value",res.petrolstartkm);
                    }else{
                        $("#petrolprice").attr("value",0);
                        $("#petrolendkm").attr("value",0);
                        $("#lastpertrolnums").attr("value",0);
                    }

                    $.ajax({
                        url:"/petrolRecord/findAllPetrol",
                        type:"post",
                        success:function (data) {
                            $("#pid").empty();
                            var op=$('<option></option>');
                            $("#pid").append(op);
                            for (var i = 0; i < data.length; i++) {
                                var optionItem = $("<option></option>");
                                optionItem.attr("value", data[i].pid);
                                if (res.pid == data[i].pid){
                                    optionItem.attr("selected", "selected");
                                }
                                optionItem.append(data[i].ptype);
                                $("#pid").append(optionItem);
                            }
                            form.render('select');
                        }
                    });
                }
            });
        });


        //动态显示车牌号码
        $.ajax({
            url: "/petrolRecord/getCar",
            type:"post",
            success:function (data) {
                var message = "<option></option>";
                $.each(data,function (index,cars) {
                    message += "<option value="+cars.carid+">"+cars.carplate+"</option>";
                });
                $("#carid").html(message);
                form.render('select');
            }
        });

        //动态展示加油站信息
        $.ajax({
            url:"/petrolRecord/findAllBtype",
            type:"post",
            success:function (data) {
                var message = "<option></option>";
                $.each(data,function (index,btype) {
                    message += "<option value="+btype.btypeid+">"+btype.btypename+"</option>";
                });
                $("#btypeid").html(message);
                form.render('select');
            }
        });

        //动态展示油料信息
        $.ajax({
            url:"/petrolRecord/findAllPetrol",
            type:"post",
            success:function (data) {
                var message = "<option></option>";
                $.each(data,function (index,petrol) {
                    message += "<option value="+petrol.pid+">"+petrol.ptype+"</option>";
                });
                $("#pid").html(message);
                form.render('select');
            }
        });


        //监听本次加油量
        $(function(){
            //输入框的值改变时触发
            $("#petrolnums").on("input",function(e){
                //获取input输入的值
                var totalprice = e.delegateTarget.value * $("#petrolprice").val();
                $("#petroltotalprice").attr("value",totalprice);
            });
        });

    })
</script>
</body>
</html>